package com.example.administrator.damaiwang.Utils;


import com.alibaba.fastjson.JSON;
import com.example.administrator.damaiwang.bean.LBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * Created by Administrator on 2016/11/18.
 */
public class HttpUtils {
    public static String getJson(String path){
        try {
            URL url =new URL(path);
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            if (connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                InputStream inputStream = connection.getInputStream();
                ByteArrayOutputStream baos=new ByteArrayOutputStream();
                int len =0;
                byte[] bs=new byte[1024];
                while ((len=inputStream.read(bs))!=-1){
                    baos.write(bs,0,len);
                }
                return baos.toString();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<LBean> parseJson(String json){
        try {
            JSONObject object =new JSONObject(json);
            JSONArray array =object.getJSONArray("l");
//            List<LBean> list =new ArrayList<>();
//            for (int i = 0; i < array.length(); i++) {
//                JSONObject object1=array.optJSONObject(i);
//                LBean bean =new LBean();
//                bean.setN(object1.optString("n"));
//                bean.setPriceName(object1.optString("priceName"));
//                bean.setT(object1.optString("t"));
//                bean.setVenName(object1.optString("VenName"));
//                bean.setIsXuanZuo(object1.optInt("IsXuanZuo"));
//                bean.setSummary(object1.optString("Summary"));
//                list.add(bean);
//            }

            List<LBean> list = JSON.parseArray(array.toString(), LBean.class);
            if (list!=null&&list.size()>0){
            return list;
            }else {
                return null;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


}
